/**
 * 签到  http://192.168.0.152:8020/bluehired/sign.html?sign=281
 * @author  Jello
 * @create  2018/12/28 16:21
 * @version 1.0
 * @company 广东蓝聘科技网络有限公司
 **/
var signCount = "";
var signObj = "";
var sgin = "";
var spinner = new Spinner(opts);

$(function() {
	sgin = getParam("sign");
	$("#signdate").html(getNowFormatDate());

	//查询用户当天是否签到
	$.ajax({
		type: "get",
		url: serverUrl + "/userSign/selectCurIsSign?sgin=" + sgin,
		cache: false,
		async: false,
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		timeout: 15000,
		crossDomain: true,
		success: function(data) {
			var resultObj = eval(data);
			if(resultObj.code == 0 && resultObj.data == 0) { //没签到
				$.ajax({
					type: "get",
					url: serverUrl + "/userSign/add?sgin=" + sgin,
					cache: false,
					async: false,
					contentType: "application/json; charset=utf-8",
					dataType: "json",
					timeout: 15000,
					crossDomain: true,
					success: function(data) {
						var resultObj = eval(data);
						if(resultObj.code == 0 && resultObj.data == 1) {
							$("#sign_score").show();
							querySignList();
						} else {
							mui.toast("签到失败");
							$("#sign_btn").show();
							return;
						}
					},
					error: function(XMLHttpRequest, status, errorThrown) {
						mui.toast("请求超时");
						return;
					}
				});
			} else if(resultObj.code == 0 && resultObj.data == 1) {
				$("#sign_score").show();
				querySignList();
			} else {
				mui.toast("网络异常");
				$("#sign_btn").show();
				return;
			}
		},
		error: function(XMLHttpRequest, status, errorThrown) {
			mui.toast("请求超时");
			return;
		}
	});

	var cale = new Calendar("idCalendar", {
		qdDay: signObj,
		onToday: function(o) {
			o.className = "onToday";
		},
		onFinish: function() {
			$$("sign-count").innerHTML = signCount; //已签到次数
		}
	});

	$("#sign_btn").click(function() {
		$.ajax({
			type: "get",
			url: serverUrl + "/userSign/selectCurIsSign?sgin=" + sgin,
			cache: false,
			async: false,
			contentType: "application/json; charset=utf-8",
			dataType: "json",
			timeout: 15000,
			crossDomain: true,
			success: function(data) {
				var resultObj = eval(data);
				if(resultObj.code == 0 && resultObj.data == 0) { //没签到
					$.ajax({
						type: "get",
						url: serverUrl + "/userSign/add?sgin=" + sgin,
						cache: false,
						async: false,
						contentType: "application/json; charset=utf-8",
						dataType: "json",
						timeout: 15000,
						crossDomain: true,
						success: function(data) {
							var resultObj = eval(data);
							if(resultObj.code == 0 && resultObj.data == 1) {
								$("#sign_score").show();
								querySignList(function() {
									var cale = new Calendar("idCalendar", {
										qdDay: signObj,
										onToday: function(o) {
											o.className = "onToday";
										},
										onFinish: function() {
											$$("sign-count").innerHTML = signCount; //已签到次数
										}
									});
								});
							} else {
								mui.toast("签到失败");
								$("#sign_btn").show();
								return;
							}
						},
						error: function(XMLHttpRequest, status, errorThrown) {
							mui.toast("请求超时");
							return;
						}
					});
				} else if(resultObj.code == 0 && resultObj.data == 1) {
					$("#sign_score").show();
					querySignList(function() {
						var cale = new Calendar("idCalendar", {
							qdDay: signObj,
							onToday: function(o) {
								o.className = "onToday";
							},
							onFinish: function() {
								$$("sign-count").innerHTML = signCount; //已签到次数
							}
						});
					});
				} else {
					mui.toast("网络异常");
					$("#sign_btn").show();
					return;
				}
			},
			error: function(XMLHttpRequest, status, errorThrown) {
				mui.toast("请求超时");
				return;
			}
		});
	})

})

/**
 * 查询签到列表
 */
function querySignList(callback) {
	$.ajax({
		type: "get",
		url: serverUrl + "/userSign/selectSignInfo?sgin=" + sgin,
		cache: false,
		async: false,
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		timeout: 15000,
		crossDomain: true,
		success: function(data) {
			var resultObj = eval(data);
			if(resultObj.code == 0 && resultObj.data != null) {
				var signList = resultObj.data.signTimeList;
				signObj = new Array();
				if(signList.length > 0) {
					for(var i = 0; i < signList.length; i++) {
						signObj.push(parseInt(signList[i].substring(8, 10)));
					}
				}
				signCount = resultObj.data.signNumber;
				$("#sign_num").html(resultObj.data.gainDayScore + "积分");
				if(callback) {
					callback();
				}
			}
		},
		error: function(XMLHttpRequest, status, errorThrown) {
			mui.toast("请求超时");
			return;
		}
	});
}